package com.ruoyi.common.core.utils.file;

import java.awt.*;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.ruoyi.common.core.domain.R;
import org.apache.poi.util.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;

/**
 * 图片处理工具类
 *
 * @author ruoyi
 */
public class ImageUtils {
    private static final Logger log = LoggerFactory.getLogger(ImageUtils.class);

    public static byte[] getImage(String imagePath) {
        InputStream is = getFile(imagePath);
        try {
            return IOUtils.toByteArray(is);
        } catch (Exception e) {
            log.error("图片加载异常 {}", e);
            return null;
        } finally {
            IOUtils.closeQuietly(is);
        }
    }

    public static InputStream getFile(String imagePath) {
        try {
            byte[] result = readFile(imagePath);
            result = Arrays.copyOf(result, result.length);
            return new ByteArrayInputStream(result);
        } catch (Exception e) {
            log.error("获取图片异常 {}", e);
        }
        return null;
    }

    /**
     * 读取文件为字节数据
     *
     * @param url 地址
     * @return 字节数据
     */
    public static byte[] readFile(String url) {
        InputStream in = null;
        try {
            // 网络地址
            URL urlObj = new URL(url);
            URLConnection urlConnection = urlObj.openConnection();
            urlConnection.setConnectTimeout(30 * 1000);
            urlConnection.setReadTimeout(60 * 1000);
            urlConnection.setDoInput(true);
            in = urlConnection.getInputStream();
            return IOUtils.toByteArray(in);
        } catch (Exception e) {
            log.error("访问文件异常 {}", e);
            return null;
        } finally {
            IOUtils.closeQuietly(in);
        }
    }

    public static Map<String, Object> getImgWidthAndHeight(String imgUrl) {
        //创建一个URL对象,获取网络图片的地址信息（网络图片）
        try {
            if (!imgUrl.contains("https") && !imgUrl.contains("http")) {
                imgUrl = "https://file.pbb2c.com/" + imgUrl;
            }

            URL url = new URL(imgUrl);
            //将URL对象输入流转化为图片对象 (url.openStream()方法，获得一个输入流)
            Image srcImg = ImageIO.read(url.openStream());
            //获取图片的宽
            int srcImgWidth = srcImg.getWidth(null);
            //获取图片的高
            int srcImgHeight = srcImg.getHeight(null);
            System.out.println("Width: " + srcImgWidth + ", Height: " + srcImgHeight);
            Map<String, Object> map = new HashMap<>();
            map.put("width", srcImgWidth);
            map.put("height", srcImgHeight);

            return map;

        } catch (IOException e) {
            e.printStackTrace();
            return null;

        }
    }

}
